package com.sdp.th.netty.server;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.sdp.th.netty.decode.StringMessageDecoder;
import com.sdp.th.netty.handler.MyHandler;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class NettyInitalizer extends ChannelInitializer<SocketChannel> {
	
	@Autowired
	private MyHandler myHandler;

	@Value("${sdp.netty.timeOut}")
	private int timeOut;
	

	@Override
	protected void initChannel(SocketChannel socketChannel) throws Exception {
		log.info("***********预设超时时间:{}",timeOut);
		ChannelPipeline pipeline = socketChannel.pipeline();
		pipeline.addLast(new IdleStateHandler(timeOut, 0, 0));
		pipeline.addLast(new LoggingHandler(LogLevel.INFO));
		pipeline.addLast(new StringMessageDecoder());
		pipeline.addLast(myHandler);

	}

}
